<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="公告类型" :labelCol="labelCol"  :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['noticeType']" :trigger-change="true" dictCode="noticeType" placeholder="请选择公告类型"/>
        </a-form-item>
        <a-form-item label="公告标题" :labelCol="labelCol" aria-required='true' :wrapperCol="wrapperCol">
          <a-input v-decorator="['noticeTitle',{rules: [{ required: true, message: '请填写公告标题！' }]}]" placeholder="请输入公告标题"></a-input>
        </a-form-item>
        <a-form-item label="公告简介" aria-required='true'  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['noticeIntroduction',{rules: [{ required: true, message: '请填写公告简介！'}]}]" rows="4" placeholder="请输入公告简介"/>
        </a-form-item>
        <a-form-item label="首页展示的图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-image-upload  v-decorator="['noticeImg']"></j-image-upload>
        </a-form-item>
        <a-form-item label="公告内容" required='true' :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-editor v-decorator="['noticeText',{trigger:'input'},{rules: [{ required: true, message: '请填写公告内容！'}]}]" />
        </a-form-item>
        <a-form-item label="添加附件" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <j-upload v-decorator="['filesPath']"></j-upload>
          <label style='color: red'>（不超过10MB）</label>
        </a-form-item>
        <a-form-item label="公告状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group buttonStyle="solid" v-decorator="[ 'noticeStatus', {'initialValue':1}]">
            <a-radio-button :value="1">显示</a-radio-button>
            <a-radio-button :value="0">隐藏</a-radio-button>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import JUpload from '@/components/jeecg/JUpload';
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'
  import JImageUpload from '@/components/jeecg/JImageUpload'

  export default {
    name: "NoticeModal",
    components: {
      JDictSelectTag,
      JEditor,
      JUpload,
      JImageUpload
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:1200,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "org/notice/add",
          edit: "org/notice/edit",
        },
      }
    },
    created () {
    },
    mounted() {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'noticeType','noticeTitle','noticeIntroduction','noticeText','noticeStatus','filesPath','noticeImg'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
          const that = this;
        let reg = 'http://124.163.201.132:8009/jeecg-boot/sys/common/static/'
        let img = that.form.getFieldValue('noticeImg')
        if (img!=null){
          let newImg = img.split(reg).pop()
          console.log(newImg)
          that.form.setFieldsValue({
            noticeImg:newImg
          })
        }
          // 触发表单验证
           this.form.validateFields((err, values) => {
            let formData = Object.assign(this.model, values);
             console.log(formData)
             if(formData.noticeText===undefined){
               this.$message.error("请填写公告内容")
             }else{
               if (!err) {
                 that.confirmLoading = true;
                 let httpurl = '';
                 let method = '';
                 if(!this.model.id){
                   httpurl+=this.url.add;
                   method = 'post';
                 }else{
                   httpurl+=this.url.edit;
                   method = 'put';
                 }
                 console.log("表单提交数据",formData)
                 httpAction(httpurl,formData,method).then((res)=>{
                   if(res.success){
                     that.$message.success(res.message);
                     that.$emit('ok');
                   }else{
                     that.$message.warning(res.message);
                   }
                 }).finally(() => {
                   that.confirmLoading = false;
                   that.close();
                 })
               }
             }


          })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'noticeType','noticeTitle','noticeIntroduction','noticeText','noticeStatus','filePath','noticeImg'))
      },


    }
  }
</script>